from ui.components.base.widget import TgBaseWidget
from .views.global_index_view import GlobalIndexView
from .controllers.global_index_controller import GlobalIndexController
from loguru import logger


class GlobalIndexTab(TgBaseWidget):
    """全球指数 Tab - MVC架构重构版本"""

    def __init__(self):
        super().__init__()
        self.view = GlobalIndexView()
        self.controller = GlobalIndexController()
        
        self._setup_mvc()
        self._initialize()

    def _setup_mvc(self):
        from ui.components.layouts.vbox_layout import TgVBoxLayout
        TgVBoxLayout(self).addWidget(self.view)
        self._connect_signals()

    def _connect_signals(self):
        self.controller.hk_data_loaded.connect(self.view.update_hk_data)
        self.controller.global_data_loaded.connect(self.view.update_global_data)

    def _initialize(self):
        pass

    def cleanup(self):
        """清理资源"""
        self.controller.cleanup()

    def showEvent(self, event):
        # 当 Tab 可见时，启动定时刷新并立即刷新一次
        self.controller.start_auto_refresh()
        self.controller.load_data()
        super().showEvent(event)

    def hideEvent(self, event):
        # 当 Tab 隐藏时，停止定时刷新
        self.controller.stop_auto_refresh()
        super().hideEvent(event)